package highlighter;

import java.io.StringReader;

import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.Scorer;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import org.wltea.analyzer.lucene.IKAnalyzer;

public class First {

	public static void main(String[] args) throws Exception {
		
		String text = "一个国家的文明程度，很重要的体现之一就在于她对弱势群体、障碍人士的入微体恤和人文关怀。";
		TermQuery query = new TermQuery(new Term("field", "人文"));
		Scorer scorer = new QueryScorer(query);
		SimpleHTMLFormatter formater = new SimpleHTMLFormatter("<span class=\"highlight\">", "</span>");
		Highlighter hig = new Highlighter(formater, scorer);
		TokenStream tokens = new IKAnalyzer().tokenStream("field", new StringReader(text));
		System.out.println(hig.getBestFragment(tokens, text));
	}
}
